package com.sskj.cndragon.presenter;

import com.luck.picture.lib.tools.Constant;
import com.lzy.okgo.model.Response;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.bean.AddressBean;
import com.sskj.cndragon.bean.AssetBean;
import com.sskj.cndragon.bean.DragonBean;
import com.sskj.cndragon.bean.NoticeBean;
import com.sskj.cndragon.bean.RushDragonBean;
import com.sskj.cndragon.bean.UpdataBean;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.ui.activity.MainActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.util.APKVersionCodeUtils;

import java.util.List;

public class MainPresenter extends BasePresenter<MainActivity> {
    public void getAsset(int userId, CoolRefreshView coolRefreshView) {
        httpService.getAsset(userId)
                .tag(this)
                .execute(new JsonCallBack<HttpData<AssetBean>>(coolRefreshView == null ? this : null) {
                    @Override
                    public void onSuccess(Response<HttpData<AssetBean>> response) {
                        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
                            coolRefreshView.setRefreshing(false);
                        }
                        HttpData<AssetBean> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.updataAsset(body.getData());
                        }
                    }
                });
    }

    public void getDragons(CoolRefreshView coolRefreshView) {
        httpService.getDragons().tag(this)
                .execute(new JsonCallBack<HttpData<List<DragonBean>>>(coolRefreshView == null ? this : null) {
                    @Override
                    public void onSuccess(Response<HttpData<List<DragonBean>>> response) {
                        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
                            coolRefreshView.setRefreshing(false);
                        }
                        HttpData<List<DragonBean>> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.updataDragons(body.getData());
                        }
                    }
                });
    }

    public void getDragonsMsg(String id, boolean ifShow) {
        httpService.getDragonMsg(id)
                .tag(this)
                .execute(new JsonCallBack<HttpData<DragonBean>>(ifShow ? this : null) {
                    @Override
                    public void onSuccess(Response<HttpData<DragonBean>> response) {
                        HttpData<DragonBean> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.updataDragonMsg(body.getData());
                        }
                    }
                });
    }

    public void reservationDragon(String dragonID, String userID) {
        httpService.reservationDragon(dragonID, userID).tag(this)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData body = response.body();
                        ToastUtil.showShort(body.getMsg());
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.reservationOK();
                        }
                    }
                });
    }

    public void buyDragon(String dragonID, String userID) {
        httpService.buyDragon(dragonID, userID)
                .tag(this)
                .execute(new JsonCallBack<HttpData<RushDragonBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<RushDragonBean>> response) {
                        HttpData<RushDragonBean> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.buyDragonOK(body.getData());
                        } else {
                            ToastUtil.showShort(body.getMsg());
                        }

                    }
                });
    }

    public void getNotices() {
        httpService.getNotices()
                .tag(this)
                .execute(new JsonCallBack<HttpData<List<NoticeBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<List<NoticeBean>>> response) {
                        HttpData<List<NoticeBean>> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.updataNotices(body.getData());
                        }
                    }
                });
    }

    public void getAddresses(String userID) {
        httpService.getAddresses(userID)
                .tag(this)
                .execute(new JsonCallBack<HttpData<List<AddressBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<AddressBean>>> response) {
                        HttpData<List<AddressBean>> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.getAddressOK(body.getData());

                        } else {
                            ToastUtil.showShort(body.getMsg());
                        }

                    }
                });
    }


    public void updata(String verName) {
        httpService.updata(verName)
                .execute(new JsonCallBack<HttpData<UpdataBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<UpdataBean>> response) {
                        HttpData<UpdataBean> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.needUpdata(body.getData());
                        }
                    }
                });

    }
}
